package com.atguigu.gmall.product.rpc;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.item.to.CategoryView;
import com.atguigu.gmall.model.item.to.SkuDetailTO;
import com.atguigu.gmall.model.product.entity.SkuImage;
import com.atguigu.gmall.model.product.entity.SkuInfo;
import com.atguigu.gmall.model.product.vo.SpuSaleInfoVo;
import com.atguigu.gmall.product.service.BaseCategory1Service;
import com.atguigu.gmall.product.service.SkuImageService;
import com.atguigu.gmall.product.service.SkuInfoService;
import com.atguigu.gmall.product.service.SpuSaleAttrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

/**
 * @Author:lyd
 **/
@RestController
@RequestMapping("/api/inner/product")
public class SkuDetailRpcController {

    @Autowired
    BaseCategory1Service baseCategory1Service;
    @Autowired
    SkuInfoService skuInfoService;
    @Autowired
    SkuImageService skuImageService;
    @Autowired
    SpuSaleAttrService spuSaleAttrService;

    @GetMapping("/categoryView/{category3Id}")
    public Result<CategoryView> getCategoryView(@PathVariable("category3Id") Long category3Id) {
        CategoryView categoryView = baseCategory1Service.getCategoryView(category3Id);
        return Result.ok(categoryView);
    }

    @GetMapping("/skuInfo/{skuId}")
    public Result<SkuInfo> getSkuInfo(@PathVariable("skuId") Long skuId) {
        SkuInfo skuinfo = skuInfoService.lambdaQuery()
                .eq(SkuInfo::getId, skuId)
                .eq(SkuInfo::getIsSale, 1)
                .one();
        return Result.ok(skuinfo);
    }

    @GetMapping("/skuPrice/{skuId}")
    Result<BigDecimal> getSkuPrice(@PathVariable("skuId") Long skuId) {
        SkuInfo skuInfo = skuInfoService.lambdaQuery()
                .select(SkuInfo::getPrice)
                .eq(SkuInfo::getId, skuId)
                .one();
        return Result.ok(skuInfo.getPrice());
    }

    @GetMapping("/skuImage/{skuId}")
    Result<List<SkuDetailTO.SkuImageVo>> getSkuImageVoList(@PathVariable("skuId") Long skuId) {
        List<SkuDetailTO.SkuImageVo> skuImageVos = skuImageService.getSkuImageVoList(skuId);
        return Result.ok(skuImageVos);
    }


    /**
     * 查询spu对应的销售属性，并根据sale_attr_value_id选中当前skuId对应的销售属性
     *
     * @param spuId
     * @return
     */
    @GetMapping("/spuSaleAttr/{spuId}/{skuId}")
    Result<List<SpuSaleInfoVo>> getSpuSaleAttrList(@PathVariable("spuId") Long spuId,
                                                   @PathVariable("skuId") Long skuId) {
        List<SpuSaleInfoVo> list = spuSaleAttrService.getSpuSaleAttrListBySkuId(spuId,skuId);
        return Result.ok(list);
    }

    /**
     * 查询spu对应的销售属性，并根据sale_attr_value_id选中当前skuId对应的销售属性
     * @param spuId
     * @return
     */
    @GetMapping("/valueSkuJson/{spuId}")
    Result<String> getValueSkuJson(@PathVariable("spuId") Long spuId){
        String valueSkuJson = spuSaleAttrService.getValueSkuJson(spuId);
        return Result.ok(valueSkuJson);
    }
}
